class CreateGoals < ActiveRecord::Migration
  def self.up
    create_table :goals do |t|
      t.string        :name,            :limit => 50
      t.string        :summary,         :limit => 200
      t.text          :description
      t.integer       :priority,        :default => 0
      t.integer       :parent_id,       :default => 0
      t.datetime      :begin_at
      t.datetime      :end_at
      t.integer       :interval,        :default => 1
      t.string        :interval_unit,   :limit => 50
      t.boolean       :active,          :default => true
      t.integer       :completed,       :default => Goal::COMPLETE_BY_DATE
      t.timestamps
    end

    create_table :goal_checkpoints do |t|
      t.references    :goal
      t.integer       :state,           :default => GoalCheckpoint::STATE_PENDING
      t.datetime      :targetted_at
      t.string        :note,            :limit => 255
      t.timestamps
    end
    
    add_index :goals, [:active, :parent_id, :priority]
    add_index :goals, [:active, :begin_at]
    add_index :goal_checkpoints, [:goal_id, :targetted_at]
    add_index :goal_checkpoints, [:goal_id, :state]
    add_index :goal_checkpoints, :state
  end

  def self.down
    drop_table :goals
    drop_table :goal_checkpoints
  end
end
